package com.hqj.demo;


import java.io.*;
import java.util.Properties;

public class FileUtil {

    public static void createDir(File f){
        if(!f.exists()){
            f.mkdirs();
        }
    }

    public static void createFile(File f){
        try{
            if(!f.exists()){
                f.createNewFile();
            }
        }catch (Exception e){
            System.out.println("新文件创建失败，错误信息："+e.getMessage());
        }
    }

    public static Object objectInput(File f){
        try {
            //声明字节输入流对象
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);

            Object obj = ois.readObject();

            //关闭
            ois.close();
            fis.close();
            return obj;
        }catch (Exception e){
            System.out.println("对象读取失败，失败信息:"+e.getMessage());
        }
        return null;
    }


    public static void objectOutput(File f,Object obj){
        try {

            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(obj);

            //关闭
            oos.close();
            fos.close();
        }catch (Exception e){
            System.out.println("对象写入失败，失败信息:"+e.getMessage());
        }
    }

    public static void proStore(File f, Properties pro){
        try{

            FileOutputStream fos = new FileOutputStream(f);
            //执行属性输出
            pro.store(fos,"the score by student");
            //关闭
            fos.close();
        }catch (Exception e){
            System.out.println("属性文件写入失败，失败信息:"+e.getMessage());
        }
    }

    public static Properties proLoad(File f){
        try{
            FileInputStream fis = new FileInputStream(f);
            //属性文件的加载
            Properties pro = new Properties();
            pro.load(fis);
            //关闭
            fis.close();
            return pro;
        }catch (Exception e){
            System.out.println("属性文件读取失败，失败信息:"+e.getMessage());
        }
        return null;
    }

}
